﻿using com.guojin.core.io.message;
using com.guojin.mj.net;
using com.guojin.mj.net.message.login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateRoomController
{
    private static CreateRoomController instance;
    public static CreateRoomController GetInstance()
    {
        if (instance == null)
        {
            instance = new CreateRoomController();
        }
        return instance;
    }

    public string betweenOnePointJvGuo = "true";
    public string cardCounts = "4";
    public  string chapterMax = "16";
    public string middleJoin = "true";
    public string scoreRange = "1-4";
    public  int[] cardCountsArr = new int[2];
    public string[] scoreRangeArr = new string[4];

    //创建房间规则返回消息
    public void OnReceiveMsg757(string msg, Message data)
    {
        CreateRoomPageRulesRet crpr = (CreateRoomPageRulesRet)data;
        CopyCardCountsToArray(crpr);
        CopyScoreRangeToArray(crpr);
        CreateRoomPage.GetInstance().ShowCardCountsInText();
        CreateRoomPage.GetInstance().ShowScoreRangeInText();
        FormMemoryRuleSetPage();
    }

    private void CopyCardCountsToArray(CreateRoomPageRulesRet crpr)
    {
        for (int i = 0; i < crpr.cardCounts.Length; i++)
        {
            cardCountsArr[i] = crpr.cardCounts[i];
        }
    }

    private void CopyScoreRangeToArray(CreateRoomPageRulesRet crpr)
    {
        for (int i = 0; i < crpr.scoreRange.Length; i++)
        {
            scoreRangeArr[i] = crpr.scoreRange[i];
        }
    }

    private void FormMemoryRuleSetPage()
    {
        CreateRoomPage.GetInstance().SetChapterMax(GameController.GetInstance().GetRuleFormPlayerPrefs(GameController.chapterMax));
        CreateRoomPage.GetInstance().SetMiddleJoin(GameController.GetInstance().GetRuleFormPlayerPrefs(GameController.middleJoin));
        CreateRoomPage.GetInstance().SetBetweenOnePointJvGuo(GameController.GetInstance().GetRuleFormPlayerPrefs(GameController.betweenOnePointJvGuo));
        CreateRoomPage.GetInstance().SetCardCounts(GameController.GetInstance().GetRuleFormPlayerPrefs(GameController.cardCounts));
        CreateRoomPage.GetInstance().SetScoreRange(GameController.GetInstance().GetRuleFormPlayerPrefs(GameController.scoreRange));
    }

    //创建房间添加规则
    public List<OptionEntry> AppendRuleToCreateRoomMsg()
    {
        var optionList = new List<OptionEntry>();
        optionList.Add(OptionEntry.create(GameController.chapterMax, chapterMax));
        optionList.Add(OptionEntry.create(GameController.middleJoin, middleJoin));
        optionList.Add(OptionEntry.create(GameController.betweenOnePointJvGuo, betweenOnePointJvGuo));
        optionList.Add(OptionEntry.create(GameController.cardCounts, cardCounts));
        optionList.Add(OptionEntry.create(GameController.scoreRange, scoreRange));
        return optionList;
    }

    //创建房间消息
    public void OnReceiveMsg71(string msg, Message data)
    {
        Debug.Log("收到创建房间信息");
        var crr = (CreateRoomRet)data;
        if (crr.result)
        {
            switch (crr.RoomType)
            {
                case 0:
                case 2:
                    MessageManager.GetInstance().SendMsg76(crr.roomID);
                    break;
                case 1: //房间类型为代开房
                    //if (GetInstance().replaceCreatRoomEvent != null)
                    //    replaceCreatRoomEvent();
                    break;
                default:
                    break;
            }
        }
    }
}
